#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
import json
import ast
from collections import defaultdict
import argparse

def extract_dict_strings(text):
    """
    从文本中提取所有类似字典格式的字符串
    支持多种格式：单引号、双引号、带换行符等
    """
    # patterns = [r"{'TimeStamp': (\d+\.\d+), 'GL': (\d+), 'Graph': (\d+), 'ark ts heap': (\d+), 'native heap': (\d+), 'AnonPage other': (\d+), 'FilePage other': (\d+), 'Total': (\d+), 'jemalloc heap': (\d+)}"]
    patterns = [
        # 宽松格式，匹配包含这些字段的任何字典
        r"\{[^{}]*'GL'[^{}]*\}",
        r"\{[^{}]*\"GL\"[^{}]*\}",
    ]
    all_matches = []
    for pattern in patterns:
        matches = re.findall(pattern, text, re.DOTALL)
        all_matches.extend(matches)
    
    return all_matches


def parse_dict_string(dict_str):
    """
    将字典字符串解析为Python字典
    """
    try:
        # 方法1: 使用ast.literal_eval (安全)
        return ast.literal_eval(dict_str)
    except:
        try:
            # 方法2: 使用json.loads (需要双引号)
            # 将单引号替换为双引号
            json_str = dict_str.replace("'", "\"")
            return json.loads(json_str)
        except:
            try:
                # 方法3: 使用eval (不安全，但作为备选)
                # 注意：在生产环境中应避免使用eval
                return eval(dict_str)
            except:
                print(f"无法解析字符串: {dict_str[:100]}...")
                return None

def process_file(filename):
    """
    处理文件并计算统计信息
    """
    try:
        with open(filename, 'r', encoding='utf-8') as file:
            content = file.read()
    except UnicodeDecodeError:
        # 如果UTF-8失败，尝试其他编码
        try:
            with open(filename, 'r', encoding='latin-1') as file:
                content = file.read()
        except Exception as e:
            print(f"读取文件失败: {e}")
            return None, None
    
    # 提取所有字典字符串
    dict_strings = extract_dict_strings(content)
    print(f"找到 {len(dict_strings)} 个匹配的字典")
    
    # 解析所有字典
    data_points = []
    for i, dict_str in enumerate(dict_strings):
        parsed_dict = parse_dict_string(dict_str)
        if parsed_dict:
            data_points.append(parsed_dict)
            if i < 5:  # 打印前5个作为示例
                print(f"示例 {i+1}: {parsed_dict}")
    
    if not data_points:
        print("未找到有效的数据点")
        return None, None
    
    # 收集所有字段的值
    field_values = defaultdict(list)
    for data_point in data_points:
        for key, value in data_point.items():
            # 只处理数值类型的值
            if isinstance(value, (int, float)):
                field_values[key].append(value)
    
    # 计算统计信息
    statistics = {}
    for field, values in field_values.items():
        if values:
            statistics[field] = {
                'count': len(values),
                'mean': sum(values) / len(values),
                'max': max(values),
                'min': min(values),
                'sum': sum(values)
            }
    
    return statistics, len(data_points)

def print_statistics(statistics, total_points):
    """
    打印统计结果
    """
    print("\n" + "="*80)
    print(f"统计结果 (基于 {total_points} 个数据点)")
    print("="*80)
    
    if not statistics:
        print("没有可用的统计数据")
        return
    
    # 按字段名排序
    sorted_fields = sorted(statistics.keys())
    
    print(f"{'字段名':<20} {'数据点数':<10} {'平均值':<15} {'最大值':<15} {'最小值':<15} {'总和':<15}")
    print("-" * 90)
    
    for field in sorted_fields:
        stats = statistics[field]
        print(f"{field:<20} {stats['count']:<10} {stats['mean']:<15.2f} {stats['max']:<15.2f} {stats['min']:<15.2f} {stats['sum']:<15.2f}")

def save_statistics(statistics, total_points, output_file):
    """
    将统计结果保存到文件
    """
    try:
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(f"统计结果 (基于 {total_points} 个数据点)\n")
            f.write("="*50 + "\n")
            
            # JSON格式保存详细数据
            output_data = {
                'total_data_points': total_points,
                'statistics': statistics
            }
            json.dump(output_data, f, indent=2, ensure_ascii=False)
        
        print(f"\n详细统计结果已保存到: {output_file}")
    except Exception as e:
        print(f"保存文件失败: {e}")

def main():
    parser = argparse.ArgumentParser(description='分析文本中的字典数据并计算统计信息')
    parser.add_argument('filename', help='要分析的文本文件路径')
    parser.add_argument('-o', '--output', help='输出结果文件路径（可选）')
    parser.add_argument('-v', '--verbose', action='store_true', help='显示详细信息')
    
    args = parser.parse_args()
    
    if args.verbose:
        print(f"开始分析文件: {args.filename}")
    
    statistics, total_points = process_file(args.filename)
    
    if statistics:
        print_statistics(statistics, total_points)
        
        if args.output:
            save_statistics(statistics, total_points, args.output)
        
        # 生成简单的统计摘要
        print("\n" + "="*50)
        print("统计摘要")
        print("="*50)
        for field, stats in statistics.items():
            print(f"{field}:\t平均值={stats['mean']:.2f},\t最大值={stats['max']:.2f},数据点={stats['count']}")

if __name__ == "__main__":
    main()